//
//  XFXPlayerView.m
//  XFXnews
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXPlayerView.h"



@interface XFXPlayerView()

@property (nonatomic, retain)AVPlayerItem *playerItem;// 设置播放器播放项目的类,设置播放视频资源
@property (nonatomic, retain)AVPlayerLayer *playerLayer;// 设置承载显示视频播放器的类
@property (nonatomic, assign)CGFloat totalDuration; // 记录总时长
@property (nonatomic, assign)BOOL isPlaying;   // 记录播放状态

@property (nonatomic, retain)UIView *topView;
@property (nonatomic, retain)UIView *bottomView;
@property (nonatomic, retain)UISlider *progressSlider;
@property (nonatomic, retain)UIButton *button;
@property (nonatomic, retain)UILabel *totalTime;
@property (nonatomic, retain)UILabel *currenTime;
@property (nonatomic, copy)NSString *currentUrlString;

@property (nonatomic, strong)UIView *playerView;


@end


@implementation XFXPlayerView


- (void)setModel:(XFXVoiceModel *)model{
    _model = model;
    NSURL *url = [NSURL URLWithString:_model.mp4_url];
    self.playerItem = [AVPlayerItem playerItemWithURL:url];
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, XFXWIDTH, 300*XFXHEIGHT/736)];
    self.playerLayer.frame = self.playerView.frame;
    self.playerView.backgroundColor = [UIColor blackColor];
    [self.playerView.layer addSublayer:self.playerLayer];
    [self addSubview:self.playerView];
    [self.player play];
    
    self.playerView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.playerView addGestureRecognizer:tap];
    
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.playerLayer.frame.size.height-30, self.playerView.frame.size.width, 30)];
    self.bottomView.backgroundColor = [UIColor grayColor];
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.playerView.frame.size.width, 50)];
    self.topView.backgroundColor = [UIColor blackColor];
    self.topView.alpha = 0.5;
    
    [self.playerView addSubview:self.topView];
    [self.playerView addSubview:self.bottomView];
    self.progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(80,0, XFXWIDTH-130, 30)];
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"iconfont-yuandian.png"] forState:UIControlStateNormal];

    
    self.progressSlider.alpha = 0.5;
    self.progressSlider.value = 0;
    [self.bottomView addSubview:self.progressSlider];
    self.progressSlider.maximumValue = 1;
    self.progressSlider.minimumValue = 0;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 20, 30, 30);
    UIImage *image = [UIImage imageNamed:@"222.png"];
    [button setImage:image forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:button];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, XFXWIDTH, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = _model.title;
    [self.topView addSubview:label];
    
    
    [self.progressSlider addTarget:self action:@selector(progressAction:) forControlEvents:UIControlEventValueChanged];
    [self showPlayerProgress];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, XFXWIDTH*0.075, self.bottomView.frame.size.height);
    [self.button setImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
    [self.bottomView addSubview:self.button];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    // 进度时间
    self.currenTime = [[UILabel alloc] initWithFrame:CGRectMake(38*XFXWIDTH/414, 0, 50*XFXWIDTH/414, 30*XFXHEIGHT/736)];
    self.currenTime.font = [UIFont systemFontOfSize:13.0];
    self.currenTime.textColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.currenTime];

    
    self.totalTime = [[UILabel alloc] initWithFrame:CGRectMake(self.progressSlider.frame.origin.x +self.progressSlider.frame.size.width + 10*XFXWIDTH/414, 0, 50*XFXWIDTH/414, 30*XFXHEIGHT/736)];
    self.totalTime.font = [UIFont systemFontOfSize:13.0];
    self.totalTime.textColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.totalTime];

}


- (void)btn:(UIButton *)btn{
    [self.delegate back];
    
}

#pragma mark -- 懒加载创建视频视图对象.
-(UIView *)playerView {
    if (nil == _playerView) {
        _playerView = [[UIView alloc] init];
        
    }

    return _playerView;
}

#pragma  mark -- 轻拍手势
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (self.bottomView.alpha==0) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.bottomView.alpha = 1;
        }];
        
       
    }else{
        
        [UIView animateWithDuration:1 animations:^{
            
            self.bottomView.alpha = 0;
        }];
    }
    
    
}

#pragma  mark -- slider
- (void)progressAction:(UISlider *)slider{
    
    double currentnTime = slider.value * self.totalDuration;
    
    CMTime time = CMTimeMake(currentnTime, 1);
    
    [self.player seekToTime:time completionHandler:^(BOOL finished) {
        
        [self.player play];
    }];
}
     
- (void)showPlayerProgress{
    
    __block XFXPlayerView *vc = self;
    CMTime time = CMTimeMake(1, 1);
    [self.player addPeriodicTimeObserverForInterval:time queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        
        CMTime currenTime = vc.playerItem.currentTime;
        CMTime totalTime = vc.playerItem.duration;
        
        double currentT = CMTimeGetSeconds(currenTime);
        double totalT = CMTimeGetSeconds(totalTime);
        
        vc.progressSlider.value = currentT/totalT;
        
        vc.totalDuration = totalT;
        
        
        CGFloat timeT = CMTimeGetSeconds(self.playerItem.duration);
        CGFloat current = CMTimeGetSeconds(self.playerItem.currentTime);
        
        CGFloat rem = timeT - current;
        
        NSString *totalTime1 = [NSString stringWithFormat:@"%02d:%02d",(int)rem/60,(int)rem%60];
        NSString *currentTime = [NSString stringWithFormat:@"%02d:%02d",(int)current/60,(int)current%60];
//        NSLog(@"----%@",totalTime1);
        
        self.currenTime.text = currentTime;
        self.totalTime.text = totalTime1;

    }];
    
}

- (void)buttonAction:(UIButton *)bnt{

    if (self.isPlaying == NO) {
        
        [self.player play];
        
        [self.button setImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
    }else{
        
        [self.player pause];
        
        [self.button setImage:[UIImage imageNamed:@"iconfont-bofangqibofang.png"] forState:UIControlStateNormal];
    }
    
    self.isPlaying = !self.isPlaying;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:1 animations:^{
            
            self.bottomView.alpha = 0;
        }];
        
    });
    
    
}





@end
